package com.graduate.work.graduatework.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.graduate.work.graduatework.contant.UrlContant;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @author: cuibingqi.
 * @date: 17/2/11 下午2:45.
 * @descripe: 抓取机票信息数据
 */
@Service
public class DataHandleUtil {

    /**
     * 抓去单程的机票详情列表
     * @param DCity1 出发城市
     * @param ACity1 到达城市
     * @param DDate1 出发日期
     */
    public JSONObject captureOwAirTicketList(String DCity1, String ACity1, String DDate1) {
        String OwUrl = UrlContant.FZ_OWFLIGHT_LIST_URL + "&depCityName=" + DCity1 + "&arrCityName=" + ACity1 + "&depDate=" + DDate1 + "&_input_charset=utf-8";
        //1.获得一个httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //2.生成一个get请求
        HttpGet httpget = new HttpGet(OwUrl);

        CloseableHttpResponse response = null;
        try {
            //3.执行get请求并返回结果
            response = httpclient.execute(httpget);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result = StringUtils.trimAllWhitespace(result);
        result = result.replace(");","");
        result = result.replace("jsonp160(","");
        System.out.println(result);
        return JSON.parseObject(result);
    }
    public JSONObject captureOwAirTicketListByOk(String DCity1, String ACity1, String DDate1) {
        String OwUrl = UrlContant.FZ_OWFLIGHT_LIST_URL + "?DCity1=" + DCity1 + "&ACity1=" + ACity1 + "&DDate1=" + DDate1;
        System.out.println(OwUrl);
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(OwUrl).build();
        Response response = null;
        String content = "";
        try {
            response = okHttpClient.newCall(request).execute();
            content = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSON.parseObject(content);
    }


}
